using Firestarter.DB.Schema.BO;
using Firestarter.DB.Schema.Test.BO.TestUtils;
using FireStarterModeller.Test;
using Habanero.Testability;
using NUnit.Framework;

namespace Firestarter.DB.Schema.Test.BO
{
    /// <summary>
    /// Provides a place to write custom tests for DBDatabase objects.
    /// This file is only written once and can be changed.  The Def file
    /// attached to this as a dependent is rewritten with each regeneration
    /// and contains the standard tests for DBDatabase.
    /// Regenerate this test project whenever there have been changes to the
    /// business objects.
    /// If tests are failing due to a unique setup in your application,
    /// you can either override the Create methods in TestUtils, or you
    /// can add the test to the ignore list below and reimplement it here.
    /// </summary>
    public partial class TestDBDatabase
    {
        [TestFixtureSetUp]
        public void SetupTestFixture()
        {
            //------------------------------------------------------------
            // Use this list to ignore generated tests that are failing
            // due to a unique condition in your application.
            // Remember to reimplement the test here.
            //------------------------------------------------------------
            //_ignoreList.Add("TestMethodName", "Reason for ignoring it");
            base.SetupFixture();
        }
        
        [Test]
        public void Test_ToString()
        {
            //---------------Set up test pack-------------------
            DBDatabase database = TestUtilsDBDatabase.CreateUnsavedValidDBDatabase();
            //---------------Assert Precondition----------------
            Assert.IsNotNull(database.DatabaseName);
//            Assert.IsNotNull(database.Catalog);
//            Assert.IsNotNull(database.Schema);
            //---------------Execute Test ----------------------
            string actualToString = database.ToString();
            //---------------Test Result -----------------------
//            string expectedToString = database.Catalog + " - " + database.Schema + " - " + database.DatabaseName;
            Assert.AreEqual(database.DatabaseName, actualToString);
//            Assert.Fail("Implement ToString() for the Business object and implement this test");
        }

        [Test]
        public void Test_GetTable_WhenHasTableShouldReturnTable()
        {
            //---------------Set up test pack-------------------
            var dbase = TestUtilsDBDatabase.GetTestFactory().WithOne(database => database.Tables).CreateValidBusinessObject();
            var expectedTable = dbase.Tables[0];
            var tableName = expectedTable.TableName;
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, dbase.Tables.Count);
            //---------------Execute Test ----------------------
            var returnedTable = dbase.GetTable(tableName);
            //---------------Test Result -----------------------
            Assert.AreSame(expectedTable, returnedTable);
        }
        [Test]
        public void Test_GetTable_WhenOneTable_DiffName_ShouldReturnNull()
        {
            //---------------Set up test pack-------------------
            var dbase = TestUtilsDBDatabase.GetTestFactory().WithOne(database => database.Tables).CreateValidBusinessObject();
            var expectedTable = dbase.Tables[0];
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, dbase.Tables.Count);
            //---------------Execute Test ----------------------
            var returnedTable = dbase.GetTable(GetRandomString());
            //---------------Test Result -----------------------
            Assert.IsNull(returnedTable);
        }

        [Test]
        public void Test_GetView_WhenHasViewShouldReturnView()
        {
            //---------------Set up test pack-------------------
            var dbase = TestUtilsDBDatabase.GetTestFactory().WithOne(database => database.Views).CreateValidBusinessObject();
            var expectedView = dbase.Views[0];
            var tableName = expectedView.TableName;
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, dbase.Views.Count);
            //---------------Execute Test ----------------------
            var returnedView = dbase.GetView(tableName);
            //---------------Test Result -----------------------
            Assert.AreSame(expectedView, returnedView);
        }
        [Test]
        public void Test_GetView_WhenOneView_DiffName_ShouldReturnNull()
        {
            //---------------Set up test pack-------------------
            var dbase = TestUtilsDBDatabase.GetTestFactory().WithOne(database => database.Views).CreateValidBusinessObject();
            var expectedView = dbase.Views[0];
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, dbase.Views.Count);
            //---------------Execute Test ----------------------
            var returnedView = dbase.GetView(GetRandomString());
            //---------------Test Result -----------------------
            Assert.IsNull(returnedView);
        }
        private static string GetRandomString()
        {
            return RandomValueGen.GetRandomString();
        }
    }
}